package com.qb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qb.entity.BlogWithTypeAndUname;
import com.qb.mapper.BlogMapper;
import com.qb.mapper.EvaluateMapper;
import com.qb.mapper.UserMapper;
import com.qb.pojo.Blog;
import com.qb.pojo.Evaluate;
import com.qb.pojo.EvaluateExample;
import com.qb.pojo.User;
import com.qb.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("blogService")
public class BlogServiceImpl implements BlogService {
    //微博mapper
    @Autowired
    private BlogMapper blogMapper;
    //用户mapper
    @Autowired
    private UserMapper userMapper;
    //评价Mapper
    @Autowired
    private EvaluateMapper evaluateMapper;

    //全查
    @Override
    public PageInfo<BlogWithTypeAndUname> findAll(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<BlogWithTypeAndUname> bolgList = blogMapper.selectBlogWithTypeAndUname();
        PageInfo<BlogWithTypeAndUname> pageInfo = new PageInfo<>(bolgList);
        return pageInfo;
    }

    //删除
    @Override
    public boolean deleteInfo(Integer bid) {

        boolean delete = blogMapper.deleteByPrimaryKey(bid) > 0 ? true : false;
        return delete;
    }
    //详情

    @Override
    public Map<String, Object> findBlogById(Integer bid) {
        //查询blog表信息
        Blog blog = blogMapper.selectByPrimaryKey(bid);
        //查询与blog关联的user表信息
        User user = userMapper.selectByPrimaryKey(blog.getuFk());
        //查询评价
        EvaluateExample example = new EvaluateExample();
        example.createCriteria().andBFkEqualTo(blog.getBid());
        List<Evaluate> list = evaluateMapper.selectByExample(example);
        //创建Map集合，存放数据
        Map<String, Object> map = new HashMap<>();
        map.put("blog", blog);
        map.put("user", user);
        map.put("evaluate", list);

        return map;
    }
}
